package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dao.UserDao;
import service.UserService;


public class UserServlet extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(UserServlet.class);
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		doGet(request, response);
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-type","text/html;charset=UTF-8");
		String method = (String)request.getParameter("method");
		if(method==null) {
			PrintWriter out = response.getWriter();
			out.println("invalid request!");
		} else if(method.equals("login")) {
			Login(request, response);
		} else if(method.equals("logout")) {
			Logout(request, response);
		} else if(method.equals("register")) {
			Register(request, response);
		}
	}

	/** 
	* @Title: Login 
	* @Description: TODO(登录) 
	* @param @param request
	* @param @param response
	* @param @throws ServletException
	* @param @throws IOException    设定参数 
	* @return void    返回类型 
	* @throws 
	* @author undoner@gmail.com 
	* @date 2017年1月23日 下午2:16:46 
	*/
	protected void Login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// get parameters
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		logger.debug("username:"+username+",password:"+password);

		// check null
		if (username == null || password == null) {
			response.sendRedirect("login.jsp");
			return;
		}

		// validate
		UserService userService = new UserService();
		boolean isValid = userService.valid(username, password);

		if (isValid) {
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			response.sendRedirect("welcome.jsp");
			return;
		} else {
			response.sendRedirect("login.jsp");
			return;
		}
	}

	/** 
	* @Title: Logout 
	* @Description: TODO(退出) 
	* @param @param request
	* @param @param response
	* @param @throws ServletException
	* @param @throws IOException    设定参数 
	* @return void    返回类型 
	* @throws 
	* @author undoner@gmail.com 
	* @date 2017年1月23日 下午2:17:13 
	*/
	protected void Logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("username");
		response.sendRedirect("login.jsp");
	}

	/** 
	* @Title: Register 
	* @Description: TODO(注册) 
	* @param @param request
	* @param @param response
	* @param @throws ServletException
	* @param @throws IOException    设定参数 
	* @return void    返回类型 
	* @throws 
	* @author undoner@gmail.com 
	* @date 2017年1月23日 下午2:17:22 
	*/
	protected void Register(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// get parameters
		String username = request.getParameter("username");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		String email = request.getParameter("email");
		logger.debug("username:"+username+",password1:"+password1+",password2:"+password2+",email:"+email);

		// check null
		if (username == null || password1 == null || password2 == null || !password1.equals(password2)) {
			response.sendRedirect("register.jsp");
			return;
		}

		// validate
		UserService userService = new UserService();
		boolean isExist = userService.isExist(username);
		if(!isExist) {
			userService.add(username, password1, email);
			response.sendRedirect("login.jsp");
			return;
		} else {
			response.sendRedirect("register.jsp");
			return;
		}
	}

}
